package me.passtime.projt;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import me.passtime.projt.account.RetrievePasswordStepOnePage;
import me.passtime.projt.db.Account;
import me.passtime.projt.db.DB;
import me.passtime.projt.reg.RegisterStepOnePage;
import me.passtime.projt.util.*;

public class Main extends Activity implements View.OnClickListener {

    private Button mRememberPwdCheckBox;
    private Button mGetPwdCheckBox;
    private EditText mPhoneNumber;
    private EditText mPassword;

    private Handler mHandler;
    private DB mDB;

    public boolean rememberMeChecked = false;
    private boolean getPwdChecked = false;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 1;
    private MainController mController;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mDB = DB.getInstance(this);
        mRememberPwdCheckBox = (Button) this.findViewById(R.id.main_remember_pwd);
        mGetPwdCheckBox = (Button) this.findViewById(R.id.main_get_pwd);
        mPhoneNumber = (EditText) this.findViewById(R.id.main_phone_number);
        mPassword = (EditText) this.findViewById(R.id.main_password);
        findViewById(R.id.main_remember_pwd).setOnClickListener(this);
        findViewById(R.id.main_get_pwd).setOnClickListener(this);

        findViewById(R.id.main_clean_btn).setOnClickListener(this);
        findViewById(R.id.register_btn).setOnClickListener(this);
        findViewById(R.id.login_btn).setOnClickListener(this);
        mHandler = new Handler();
        initRememberAccount();
        mController = new MainController(this);
    }


    private void initRememberAccount() {
        mDB.open();
        final Account account = mDB.queryRememberAccount();
        if (account != null) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mPhoneNumber.setText((CharSequence) account.phoneNumber);
                    mPassword.setText((CharSequence) account.password);
                    rememberMeChecked = true;
                    mRememberPwdCheckBox.setBackgroundDrawable(getResources().getDrawable(R.drawable.radio_02));
                }
            });
        }
        mDB.close();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_clean_btn:
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mPhoneNumber.setText("");
                    }
                });
                break;
            case R.id.login_btn:
                doLogin();
                break;
            case R.id.register_btn:
                gotoRegisterPage();
                break;
            case R.id.main_remember_pwd:
                rememberMeChecked = !rememberMeChecked;
                getPwdChecked = false;
                if (rememberMeChecked) {
                    mRememberPwdCheckBox.setBackgroundDrawable(getResources().getDrawable(R.drawable.radio_02));
                    mGetPwdCheckBox.setBackgroundDrawable(getResources().getDrawable(R.drawable.radio_01));
                } else {
                    mRememberPwdCheckBox.setBackgroundDrawable(getResources().getDrawable(R.drawable.radio_01));
                }
                break;
            case R.id.main_get_pwd:
                getPwdChecked = !getPwdChecked;
                rememberMeChecked = false;
                if (getPwdChecked) {
                    mGetPwdCheckBox.setBackgroundDrawable(getResources().getDrawable(R.drawable.radio_02));
                    mRememberPwdCheckBox.setBackgroundDrawable(getResources().getDrawable(R.drawable.radio_01));
                } else {
                    mGetPwdCheckBox.setBackgroundDrawable(getResources().getDrawable(R.drawable.radio_01));
                }
                break;
        }
    }


    private void doLogin() {
        if ("".equals(mPhoneNumber.getText().toString().trim()) || "".equals(mPassword.getText().toString().trim())) {
            AlertDialogUtils.showConfirmDiaLog(this, "请输入手机号和密码");
            return;
        } else {
            if (!CheckUtils.isMobileNo(mPhoneNumber.getText().toString().trim())) {
                AlertDialogUtils.showConfirmDiaLog(this, "请输入正确的手机号");
                return;
            }
            if (getPwdChecked) {
                PageJumpUtils.jump(this, RetrievePasswordStepOnePage.class);
            } else {
                showDialog(DIALOG_PROGRESS);
                String phoneNumber = mPhoneNumber.getText().toString().trim();
                String password = mPassword.getText().toString().trim();
                mController.login(phoneNumber, password);
            }
        }

    }


    private void gotoRegisterPage() {
        Log.d(Const.TAG, "go to Reg");
        UserHolder.phoneNumber = mPhoneNumber.getText().toString();
        PageJumpUtils.jump(this, RegisterStepOnePage.class);
    }

    @Override
    public void onBackPressed() {
        AlertDialogUtils.showYesNoDiaLog(this, "是否退出系统?", new AlertDialogUtils.Executor() {
            @Override
            public void execute() {
                SystemUtils.killSelf();
                finish();
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(Main.this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;

            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
